package cn.ybk.pet.service.impl;

import cn.ybk.basic.jwt.UserInfo;
import cn.ybk.basic.util.BaiduAuditUtils;
import cn.ybk.basic.util.LoginContext;
import cn.ybk.org.domain.Employee;
import cn.ybk.org.mapper.EmployeeMapper;
import cn.ybk.pet.domain.Pet;
import cn.ybk.pet.domain.PetDetail;
import cn.ybk.pet.domain.PetOnlineAuditLog;
import cn.ybk.pet.mapper.PetDetailMapper;
import cn.ybk.pet.mapper.PetMapper;
import cn.ybk.pet.mapper.PetOnlineAuditLogMapper;
import cn.ybk.pet.service.IPetService;
import cn.ybk.basic.service.impl.BaseServiceImpl;
import cn.ybk.user.domain.Logininfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.concurrent.ConcurrentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import sun.text.CollatorUtilities;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    // 添加
    @Override
    public void add(Pet pet) {
        pet.setCreatetime(new Date());
        pet.setState(0);
        //   通过loginContext 获取店铺id
        Logininfo logininfo = LoginContext.getLogininfo();
        Employee employee = employeeMapper.findByLoginfoId(logininfo.getId());
        pet.setShopId(employee.getShopId());
//        pet.setShopId(41L);
        // todo 店铺id
        super.add(pet);
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.add(detail);

    }

    // 修改
    @Override
    public void update(Pet pet) {
            // 更新pet
            super.update(pet);
            // 更新detail
            petDetailMapper.update(pet.getDetail());

    }

    // 上架
    @Override
    public void onsale(List<Long> ids) {
        // 校验数据
        List<Pet> pets = petMapper.findByIds(ids);
        if (CollectionUtils.isEmpty(pets)) {
            return;
        }
        // 上架的不管，下架改为上架，
        // 修改状态
        // 百度ai校验数据
        ArrayList<PetOnlineAuditLog> petOnlineAuditLogs = new ArrayList<>();
        ArrayList<Long> petId = new ArrayList<>();
        pets.forEach(pet -> {
            StringBuilder sb = new StringBuilder();
            if (!BaiduAuditUtils.TextCensor(pet.getName())) {
                sb.append("宠物名称违规：").append(pet.getName()).append(",");
            }
            String resources = pet.getResources();
            if (StringUtils.isNotBlank(resources)) {
                String[] split = resources.split(",");
                for (int i = 0; i < split.length; i++) {
                    if (!BaiduAuditUtils.ImgCensor(split[i])) {
                        sb.append("宠物图片违规").append(pet.getId());
                    }
                }
            }
            if (sb.length() > 0) {
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setAuditTime(new Date());
                // 管理员审核id
                log.setAuditId(-1L);
                log.setNote(sb.toString());
                log.setState(0);
                log.setPetId(pet.getId());
                petOnlineAuditLogs.add(log);
                return;
            }
            petId.add(pet.getId());
        });
        // 修改状态
        if (!CollectionUtils.isEmpty(petId)) {
            // 批量修改
            petMapper.batchUpdate(petId);
        }
        // 添加日志--只有审核不通过才添加
        if (!CollectionUtils.isEmpty(petOnlineAuditLogs)) {
            // 批量添加
            petOnlineAuditLogMapper.batchAdd(petOnlineAuditLogs);
        }
    }

    // 下架
    @Override
    public void offsale(List<Long> ids) {
        // sql语句根据state = 1 和id查询数据
        List<Pet> pets = petMapper.findByState(ids);
        if (CollectionUtils.isEmpty(pets)) {
            return;
        }
        // 为了添加数据库
        ArrayList<PetOnlineAuditLog> petOnlineAuditLogs = new ArrayList<>();
        ArrayList<Long> petId = new ArrayList<>();
        pets.forEach(pet -> {
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            log.setAuditId(-1L);
            log.setPetId(pet.getId());
            log.setState(0);
            log.setAuditTime(new Date());
            log.setNote("商品下架！~");
            petOnlineAuditLogs.add(log);
            petId.add(pet.getId());
        });
        if (!CollectionUtils.isEmpty(petId)) {
            // 批量修改
            System.out.println(petId);
            petMapper.batchUpdateLog(petId);
        }
        // 添加日志
        if (!CollectionUtils.isEmpty(petOnlineAuditLogs)) {
            // 批量添加
            petOnlineAuditLogMapper.batchAdd(petOnlineAuditLogs);
        }

    }
}
